cc.Class({
  extends: cc.Component,

  properties: {
    speed: 0,
    speedMoveUp: 0,
    speedMoveDown: 0,
    moveRandom: 0,
    xingRandom: 0,
    score01: 0,
    score02: 0,
    score03: 0,
    dieHeight: 0,

    jumpAudio01: {
      default: null,
      url: cc.AudioClip
    },

    jumpAudio02: {
      default: null,
      url: cc.AudioClip
    },

    jumpAudio03: {
      default: null,
      url: cc.AudioClip
    },
  },

  // LIFE-CYCLE CALLBACKS:

  onLoad() {

    this.body = this.node.getComponent(cc.RigidBody);
    this.body.enabledContactListener = true;
    // this.sp = this.node.getChildByName("bell").getComponent(cc.Sprite);
    // window.Game.player
    this.imgType = 0;

    var spine = this.spine = this.node.getChildByName("bell").getComponent('sp.Skeleton');
    let self = this;
    self.spine.setCompleteListener((trackEntry, loopCount) => {
      const animationName = trackEntry.animation ? trackEntry.animation.name : "";
      if (animationName === self.dieAm) {

        this.node.getChildByName("bell").active = false;


        self.isDie = true;
      }
    });
    this.moveDirection = 0;
  },

  onDestroy() {
  },

  update(dt) {
    if (window.Game.player.getComponent("Player").isbejump === false) {
      this.node.y -= this.speed * dt;
    }
    if (this.imgType === 1) {
      if (this.moveDirection === 0) {
        this.node.x -= this.speedMove * dt;
        if (this.node.x <= (-cc.winSize.width * 0.5 + 60)) {
          this.moveDirection = 1;
        }

      } else {
        this.node.x += this.speedMove * dt;
        if (this.node.x >= (cc.winSize.width * 0.5 - 60)) {
          this.moveDirection = 0;
        }
      }
    }

    let pos = this.node.parent.convertToWorldSpaceAR(this.node);
    let playerPos = window.Game.player.parent.convertToWorldSpaceAR(window.Game.player);
    if ((playerPos.y - pos.y) > cc.winSize.height * 0.5 || pos.y < cc.winSize.height * this.dieHeight * 0.01) {
      // this.putSelf();

      if (!this.isPutSelf) {
        this.isPutSelf = true

        this.moveCallFunc();


      }


    }


  },

  // 只在两个碰撞体开始接触时被调用一次
  onBeginContact: function (contact, selfCollider, otherCollider) {
    // var action = cc.fadeOut(0.2);
    // this.node.getChildByName("bell").runAction(action)

    this.do_die();

    if (!this.isDie) {
      var act1 = cc.fadeIn(0.1);
      var act2 = cc.delayTime(0.8);
      var act3 = cc.fadeOut(0.5);
      var seq = cc.sequence(act1, act2, act3);
      let label = this.node.getChildByName("score");
      label.runAction(seq);
      let str = label.getComponent(cc.Label);
      if (this.imgType === 1) {
        str.string = "+" + this.score01;
        Game.curScene.setScore(this.score01);
        cc.audioEngine.playEffect(this.jumpAudio02, false);
      } else if (this.imgType === 2) {
        str.string = "+" + this.score02;
        Game.curScene.setScore(this.score02);
        cc.audioEngine.playEffect(this.jumpAudio03, false);
      } else {
        str.string = "+" + this.score03;
        Game.curScene.setScore(this.score03);
        cc.audioEngine.playEffect(this.jumpAudio01, false);
      }

    }
  },

  // 获取最小值到最大值之前的整数随机数
  GetRandomNum: function (Min, Max) {
    var Range = Max - Min;
    var Rand = Math.random();
    return (Min + Math.round(Rand * Range));
  },

  init: function (smallBellMgr, smallBellObject) {


    this.smallBellMgr = smallBellMgr;
    this.smallBellObject = smallBellObject;

    this.speedMove = this.GetRandomNum(this.speedMoveDown, this.speedMoveUp);


    let num = this.GetRandomNum(1, 9999);
    // cc.log("num  ==========================================  "+num);
    if (num > 0 && num < this.moveRandom && window.Game.player.getComponent("Player").isbejump === true) {
      this.imgType = 1;
      this.standAm = "yu_stand";
      this.dieAm = "yu_die";


    } else if (num > this.moveRandom && num < this.moveRandom + this.xingRandom && window.Game.curScene.eatIndex > 20) {
      this.imgType = 2;
      this.standAm = "xing_stand"
      this.dieAm = "xing_die";
    } else {
      this.imgType = 0;
      // this.standAm = "yun_stand"
      // this.dieAm = "yun_die";
      this.attNum = this.GetRandomNum(1, 5);
      if (this.attNum === 1) {
        this.standAm = "yun1_stand"
        this.dieAm = "yun1_die";
      } else if (this.attNum === 2) {
        this.standAm = "yun2_stand"
        this.dieAm = "yun2_die";
      } else if (this.attNum === 3) {
        this.standAm = "yun3_stand"
        this.dieAm = "yun3_die";
      } else if (this.attNum === 4) {
        this.standAm = "yun4_stand"
        this.dieAm = "yun4_die";
      } else if (this.attNum === 5) {
        this.standAm = "yun5_stand"
        this.dieAm = "yun5_die";
      }


    }

    this.do_stand();

    // this.spine.setAttachment("yun=", "yun_4");

    // this.attNum = this.GetRandomNum(1,5);
    // if(this.imgType == 0){
    //     this.spine.setAttachment("yun=", "yun_"+this.attNum);
    // }

    // var act1 = cc.fadeIn(0.1);
    // this.node.getChildByName("bell").runAction(act1);

    this.node.getChildByName("bell").active = true;

    var act2 = cc.fadeOut(0.1);
    this.node.getChildByName("score").runAction(act2);

    // this.node.getChildByName("score").active = false;

    this.isDie = false;
    this.isPutSelf = false


  },

  do_stand() {
    // var oldAnim = this.spine.animation;
    // if(oldAnim != this.standAm){
    this.spine.setAnimation(0, this.standAm, true)
    // }
  },

  do_die() {
    // var oldAnim = this.spine.animation;
    // if(oldAnim != this.dieAm){
    this.spine.setAnimation(0, this.dieAm, false)
    // }
  },

  putSelf: function () {
    if (!this.isPutSelf) {
      this.isPutSelf = true

      this.do_die();

    }


  },

  isdie() {
    return this.isDie
  },

  moveCallFunc() {
    this.smallBellMgr.smallBellPool.put(this.node);

    this.smallBellMgr.addSmallBell();
  }


});
